package com.tpp.scheduler.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tpp.comm.pojo.TpCinema;
import com.tpp.comm.pojo.TpSchedulerSeat;
import com.tpp.comm.pojo.TpSchedulerSeatPrice;
import com.tpp.scheduler.mapper.SchedulerSeatPriceMapper;
import com.tpp.scheduler.vo.SeatVo;
import com.tpp.scheduler.feign.CinemaFeign;
import com.tpp.scheduler.mapper.SchedulerMapper;
import com.tpp.scheduler.mapper.SchedulerSeatMapper;
import com.tpp.scheduler.service.SchedulerSeatService;
import com.tpp.scheduler.vo.SeatDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SchedulerSeatServiceImpl implements SchedulerSeatService {

    @Resource
    private SchedulerSeatMapper schedulerSeatMapper;

    @Resource
    private SchedulerSeatPriceMapper schedulerSeatPriceMapper;

    @Resource
    private SchedulerMapper schedulerMapper;

    @Resource
    private CinemaFeign cinemaFeign;

    @Override
    public SeatVo queryOriginCinemaSeatSeatArray(Integer scheduleId) {
        LambdaQueryWrapper<TpSchedulerSeat> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TpSchedulerSeat::getScheduleId,scheduleId);
        wrapper.orderByAsc(TpSchedulerSeat::getSort);
        List<TpSchedulerSeat> tpSchedulerSeats = schedulerSeatMapper.selectList(wrapper);
        SeatVo seatVo = new SeatVo();
        List<String> seatArray = new ArrayList<>();
        seatVo.setSeatArray(seatArray);
        Map<Integer, List<TpSchedulerSeat>> collect = tpSchedulerSeats.stream().collect(Collectors.groupingBy(TpSchedulerSeat::getX));
        collect.forEach((k,seatList)->{
            List<String> asd = seatList.stream().map(seat -> {
                if (seat.getAreaLevel() == 0) {
                    return "_".toString();
                } else {
                    return Character.toString(seat.getAreaLevel() + 64);
                }
            }).toList();
            seatArray.add(String.join("",asd));
        });
        return seatVo;
    }

    @Override
    public SeatDetailVo queryCinemaSeatArrayBySchedulerId(Integer scheduleId, Integer cinemaId) {
        SeatDetailVo seatDetailVo = new SeatDetailVo();
        seatDetailVo.setScheduleId(scheduleId);
        seatDetailVo.setCinemaId(cinemaId);
        LambdaQueryWrapper<TpSchedulerSeatPrice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TpSchedulerSeatPrice::getScheduleId, scheduleId);
        List<TpSchedulerSeatPrice> tpSchedulerSeatPrices = schedulerSeatPriceMapper.selectList(wrapper);
        List<SeatDetailVo.SeatPrice> seatPrices = tpSchedulerSeatPrices.stream().map(sp->{
            SeatDetailVo.SeatPrice seatPrice = new SeatDetailVo.SeatPrice();
            seatPrice.setAreaLevelName(Character.toString(sp.getAreaLevel()+64));
            seatPrice.setPrice(sp.getPrice());
            return seatPrice;
        }).toList();
        seatDetailVo.setSeatPriceList(seatPrices);//设置价格列表
        LambdaQueryWrapper<TpSchedulerSeat> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(TpSchedulerSeat::getScheduleId,scheduleId);
        wrapper1.orderByAsc(TpSchedulerSeat::getSort);
        //根据cinemaid查询影院信息
        TpCinema cinema = cinemaFeign.findById(cinemaId);

        List<TpSchedulerSeat> tpSchedulerSeats = schedulerSeatMapper.selectList(wrapper1);
        List<SeatDetailVo.SeatInfo> seatInfos = tpSchedulerSeats.stream().map(tpSchedulerSeat -> {
            SeatDetailVo.SeatInfo seatInfo = new SeatDetailVo.SeatInfo();
            BeanUtils.copyProperties(tpSchedulerSeat,seatInfo);
            seatInfo.setCinemaId(cinemaId);
            seatInfo.setCinemaName(cinema.getName());
            String abc = Character.toString(tpSchedulerSeat.getAreaLevel()+64);
            seatInfo.setAreaLevel(abc);
            SeatDetailVo.SeatPrice seatPrice1 = seatPrices.stream().filter(seatPrice -> abc.equals(seatPrice.getAreaLevelName())).
                    findFirst().orElse(new SeatDetailVo.SeatPrice());
            seatInfo.setPrice(seatPrice1.getPrice());
            return seatInfo;
        }).toList();
        seatDetailVo.setSeatInfoList(seatInfos);
        return seatDetailVo;
    }
}
